package com.demonzym.framework.net;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

public class HttpGetListener implements HttpTaskCallBack {
	private Handler mHandler;
	private HttpGetCallBack mImageCallBack;
	
	public HttpGetListener(HttpGetCallBack callback){
		mImageCallBack = callback;
		if(Looper.myLooper() == null){
			mHandler = new InternalHandler(Looper.getMainLooper());
		}
		else{
			mHandler = new InternalHandler(Looper.myLooper());
		}
	}
	
	@Override
	public void TaskEnd(boolean success) {
		postTaskEnd(success);
	}
	
	@Override
	public void TaskEnd(boolean success, Object o) {
	}

	@Override
	public void downloadProcess(int max, int progress) {
	}

	@Override
	public void uploadProcess(int max, int progress) {
	}	


	private void postTaskEnd(boolean success) {		
		mHandler.obtainMessage(HTTP_GET_END, success).sendToTarget();
	}
	
	public interface HttpGetCallBack{
		public void taskEnd(boolean success);
	}
	
	private static final int HTTP_GET_END = 0x1;
	private class InternalHandler extends Handler {
		public InternalHandler(Looper looper) {
			super(looper);
		}

		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case HTTP_GET_END:
				mImageCallBack.taskEnd((Boolean) msg.obj);
				break;
			}
			super.handleMessage(msg);
		}
	}

}
